REST API Integration হলো Salesforce-এ RESTful APIs ব্যবহার করে অন্য সিস্টেমের সাথে ডেটা আদান-প্রদানের একটি পদ্ধতি। REST API Integration এর মাধ্যমে HTTP মেথড যেমন GET, POST, PUT, এবং DELETE ব্যবহার করে ডেটা প্রক্রিয়াকরণ করা হয়। REST API Integration সাধারণত JSON ফরম্যাটে ডেটা পাঠায় এবং গ্রহণ করে, যা ব্যবহারকারী এবং সিস্টেমের মধ্যে দ্রুত ও সহজ ডেটা এক্সচেঞ্জ নিশ্চিত করে।
HTTP মেথডের মাধ্যমে REST API-তে বিভিন্ন CRUD (Create, Read, Update, Delete) অপারেশন করা হয়:
GET মেথড ব্যবহার করে নির্দিষ্ট ডেটা রিট্রিভ করা হয়। এটি সাধারণত URL এর মাধ্যমে ডেটা পাঠায় এবং রেসপন্সে JSON ফরম্যাটে ডেটা রিটার্ন করে।
public class RESTGetExample {
public static void getAccount(String accountId) {
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://externalapi.com/api/accounts/' + accountId);
request.setMethod('GET');
request.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
HttpResponse response = http.send(request);
if (response.getStatusCode() == 200) {
System.debug(response.getBody());
} else {
System.debug('Error: ' + response.getStatusCode() + ' - ' + response.getStatus());
}
}
}
POST মেথড ব্যবহার করে নতুন ডেটা তৈরি করা হয়। এটি সাধারণত JSON বা XML ফরম্যাটে ডেটা রিকোয়েস্ট বডিতে প্রেরণ করে।
public class RESTPostExample {
public static void createAccount(String name, String industry) {
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://externalapi.com/api/accounts');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json');
request.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
String requestBody = JSON.serialize(new Map<String, Object>{
'name' => name,
'industry' => industry
});
request.setBody(requestBody);
HttpResponse response = http.send(request);
if (response.getStatusCode() == 201) {
System.debug('Account Created: ' + response.getBody());
} else {
System.debug('Error: ' + response.getStatusCode() + ' - ' + response.getStatus());
}
}
}
application/json
কনটেন্ট টাইপ সেট করা হয়েছে।PUT মেথড ব্যবহার করে বিদ্যমান ডেটা আপডেট করা হয়। এটি সাধারণত JSON ফরম্যাটে ডেটা পাঠায় এবং সার্ভারে ডেটা আপডেট করে।
public class RESTPutExample {
public static void updateAccount(String accountId, String name, String industry) {
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://externalapi.com/api/accounts/' + accountId);
request.setMethod('PUT');
request.setHeader('Content-Type', 'application/json');
request.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
String requestBody = JSON.serialize(new Map<String, Object>{
'name' => name,
'industry' => industry
});
request.setBody(requestBody);
HttpResponse response = http.send(request);
if (response.getStatusCode() == 200) {
System.debug('Account Updated: ' + response.getBody());
} else {
System.debug('Error: ' + response.getStatusCode() + ' - ' + response.getStatus());
}
}
}
DELETE মেথড ব্যবহার করে নির্দিষ্ট ডেটা রেকর্ড মুছে ফেলা হয়। এটি URL এর মাধ্যমে রেকর্ডের আইডি ব্যবহার করে অপারেশন সম্পন্ন করে।
public class RESTDeleteExample {
public static void deleteAccount(String accountId) {
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://externalapi.com/api/accounts/' + accountId);
request.setMethod('DELETE');
request.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
HttpResponse response = http.send(request);
if (response.getStatusCode() == 204) {
System.debug('Account Deleted');
} else {
System.debug('Error: ' + response.getStatusCode() + ' - ' + response.getStatus());
}
}
}
Exception Handling: REST API কলের সময় Exception Handling নিশ্চিত করতে try-catch
ব্লক ব্যবহার করুন।
try {
// HTTP request and response
} catch (Exception e) {
System.debug('Error occurred: ' + e.getMessage());
}
Proper Authentication: API কলের জন্য উপযুক্ত Authentication (যেমন OAuth 2.0 বা Bearer Token) নিশ্চিত করুন।
Limit API Calls: API কলের সংখ্যা সীমিত করুন এবং খুব প্রয়োজনীয় হলে ব্যবহার করুন, যাতে গভর্নর লিমিট অতিক্রম না করে।
Use Caching (If Possible): ডেটা ফ্রিকোয়েন্টলি পরিবর্তন না হলে ক্যাশিং ব্যবহার করুন, যা সার্ভারের লোড কমায়।
Test Coverage নিশ্চিত করুন: প্রতিটি REST API মেথডের জন্য টেস্ট ক্লাস লিখে ৭৫% কোড কাভারেজ নিশ্চিত করুন।
public class AccountAPIService {
public static String createAccount(String name, String industry) {
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://externalapi.com/api/accounts');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json');
request.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
String requestBody = JSON.serialize(new Map<String, Object>{
'name' => name,
'industry' => industry
});
request.setBody(requestBody);
HttpResponse response = http.send(request);
if (response.getStatusCode() == 201) {
return response.getBody();
} else {
throw new CalloutException('Failed to create account. Status: ' + response.getStatusCode());
}
}
}
@isTest
public class AccountAPIServiceTest {
@isTest
static void testCreateAccount() {
Test.startTest();
HttpResponse mockResponse = new HttpResponse();
mockResponse.setStatusCode(201);
mockResponse.setBody('{"id":"0011j00000XXXXXXX"}');
Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator(mockResponse));
String result = AccountAPIService.createAccount('Test Account', 'Technology');
System.assertNotEquals(null, result, 'Account ID should not be null');
Test.stopTest();
}
// Mock Class for HTTP Callout
public class MockHttpResponseGenerator implements HttpCalloutMock {
private HttpResponse response;
public MockHttpResponseGenerator(HttpResponse response) {
this.response = response;
}
public HttpResponse respond(HttpRequest req) {
return response;
}
}
}
REST API Integration Salesforce-এ HTTP Methods ব্যবহার করে ডেটা প্রক্রিয়াকরণের একটি কার্যকর পদ্ধতি। Apex-এর মাধ্যমে GET, POST, PUT, এবং DELETE মেথড ব্যবহার করে API কল করে ডেটা এক্সচেঞ্জ করা যায়। Exception Handling, Proper Authentication, এবং Test Coverage নিশ্চিত করার মাধ্যমে একটি নিরাপদ এবং নির্ভরযোগ্য API Integration নিশ্চিত করা যায়।
Read more